package cn.itcast.usermanage.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.itcast.usermanage.pojo.PageResult;
import cn.itcast.usermanage.pojo.PageStatusResult;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;

//@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("/list")
	@ResponseBody
	public PageResult<User> queryUserListBypage(@RequestParam(value="page",defaultValue="1")Integer page,
			@RequestParam(value="rows",defaultValue="5")Integer rows){
		return userService.queryUserListBypage(page,rows);
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public PageStatusResult saveUser(User user){
		try {
			userService.saveUser(user);
			return PageStatusResult.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return PageStatusResult.error();
		}
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public PageStatusResult deleteUser(@RequestParam("ids")Long[] ids){
		try {
			userService.deleteUserByIds(ids);
			return PageStatusResult.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return PageStatusResult.error();
		}
	}
	
	@RequestMapping("/export/excel")
	public ModelAndView exportExcel(
			@RequestParam(value="page")Integer page,
			@RequestParam(value="rows")Integer rows
			){
		ModelAndView mv = new ModelAndView("excelView");
		//查询分页信息
		PageResult<User> result = userService.queryUserListBypage(page, rows);
		mv.addObject("users",result.getRows());
		return mv;
	}
	
	
}
